package ajk;

import static org.junit.Assert.assertTrue;
import robocode.control.events.BattleCompletedEvent;
import robocode.control.BattlefieldSpecification;
import robocode.control.events.TurnEndedEvent;
import robocode.control.snapshot.IRobotSnapshot;
import robocode.control.testing.RobotTestBed;

/**
 * Tests that WallSpin spends at least 50% of the time on the walls when facing Crazy.
 * 
 * @author Anthony Kinsey
 * 
 */
public class TestWallSpinWallPercentageMovement extends RobotTestBed {
  /** number of turns. **/
  double turns = 0;
  /** number of times againts wall. **/
  double nearWall = 0;

  BattlefieldSpecification b = new BattlefieldSpecification();

  /**
   * Specifies that Crazy and WallSpin are to be matched up in this test case.
   * 
   * @return The comma-delimited list of robots in this match.
   */
  @Override
  public String getRobotNames() {
    return "sample.Crazy,ajk.WallSpin";
  }

  /**
   * This test runs for 1 round.
   * 
   * @return The number of rounds.
   */
  @Override
  public int getNumRounds() {
    return 10;
  }

  /**
   * After each turn, check to see if we're on a wall.
   * 
   * @param event Info about the current state of the battle.
   */
  @Override
  public void onTurnEnded(TurnEndedEvent event) {
    IRobotSnapshot robot = event.getTurnSnapshot().getRobots()[1];
    double xPos = robot.getX();
    double yPos = robot.getY();
    double bottomWall = 40;
    double topWall = b.getHeight() - 40;
    double leftWall = 40;
    double rightWall = b.getWidth() - 40;
    // Checks to see if the robot is on the walls
    if (yPos < bottomWall || yPos > topWall || xPos < leftWall || xPos > rightWall) {
      nearWall++;
    }
    turns++;
  }

  /**
   * After the battle, check to see that the robot spent at least 50% of the time riding the walls.
   * 
   * @param event Details about the completed battle.
   */
  @Override
  public void onBattleCompleted(BattleCompletedEvent event) {
    System.out.println(nearWall);
    System.out.println(turns);
    System.out.println(nearWall / turns);
    assertTrue("Check Percentage of Walling", (nearWall / turns) > 0.5);

  }

}
